//
//  GKHomeNetManager.swift
//  MySwiftObject
//
//  Created by wangws1990 on 2019/9/9.
//  Copyright © 2019 wangws1990. All rights reserved.
//

import UIKit
struct GKLoadOptions :OptionSet {
    public var rawValue: UInt
    
    static var None     : GKLoadOptions{return GKLoadOptions(rawValue: 0)}
    static var DataNet  : GKLoadOptions{return GKLoadOptions(rawValue: 1<<1)};
    static var Database : GKLoadOptions{return GKLoadOptions(rawValue: 1<<2)};
    static var Default  : GKLoadOptions{return GKLoadOptions(rawValue: DataNet.rawValue|Database.rawValue)}
}
let netManager = GKHomeNetManager();
class GKHomeNetManager: NSObject {
    
    var completion : ((_ options :GKLoadOptions) -> Void)!;
    
    lazy var listData: [GKHomeInfo] = {
        return []
    }()
    lazy var arrayData : [GKHomeInfo] = {
        return []
    }()
    var bookCase : GKHomeInfo!;
    class var manager : GKHomeNetManager{
        return netManager;
    }
     func homeNet(listData:NSArray,options:GKLoadOptions,sucesss:@escaping ((_ datas : NSArray) ->Void),failure:@escaping ((_ error : String) ->Void)){
        let group : DispatchGroup = DispatchGroup.init();
        var value = options.rawValue & GKLoadOptions.DataNet.rawValue;
        let userData :GKUserModel = GKUserManager.getModel();
        if value == 2 {
            self.arrayData.removeAll();
            for obj in userData.rankDatas{
                let model :GKRankModel = obj as GKRankModel;
                group.enter();
                GKHomeNetManager.homeHot(rankId:model._id ?? "5a6844aafc84c2b8efaa6b6e", sucesss: { (object) in
                    if object is NSDictionary{
                        let dic:NSDictionary = object as! NSDictionary;
                        let rank :NSDictionary = dic .object(forKey: "ranking") as! NSDictionary
                        let info :GKHomeInfo = GKHomeInfo.deserialize(from: rank)!;
                        info.state = GKHomeInfoState.DataNet
                        self.arrayData.append(info);
                    }
                    group.leave()
                }) { (error) in
                    group.leave()
                }
            }
        }
        value = options.rawValue & GKLoadOptions.Database.rawValue;
        if value == 4 {
            group.enter();
            GKBookCaseDataQueue.getBookModels { (datas) in
                if datas.count > 0{
                    let info :GKHomeInfo = GKHomeInfo();
                    info.state = GKHomeInfoState.Database
                    info.books = (datas as! [GKBookModel]);
                    info.title = "我的书架"
                    info.shortTitle = "我的书架"
                    self.bookCase = info;
                }
                group.leave()
            }
        }
        group.notify(queue: DispatchQueue.main) {
            self.listData.removeAll();
            if self.bookCase != nil{
                self.listData.append(self.bookCase);
            }
            if self.arrayData.count > 0 {
                self.listData.append(contentsOf: self.arrayData);
            }
            if self.listData.count > 0{
                sucesss(self.listData as NSArray) ;
            }else{
                failure("数据空空如也");
            }
        }
    }
    class func homeHot(rankId:String,sucesss:@escaping ((_ object : AnyObject) ->Void),failure:@escaping ((_ error : String) ->Void)){
        let url = "ranking/"+(rankId);
        BaseNetManager.iGetUrlString(urlString:BaseNetManager.hostUrl(txcode:url), parameters:[:], sucesss: sucesss, failure: failure);
    }
    class func homeSex(sucesss:@escaping ((_ object : AnyObject) -> Void),failure:@escaping ((_ error : String) ->Void)){
        BaseNetManager.iGetUrlString(urlString:BaseNetManager.hostUrl(txcode:"ranking/gender"), parameters:[:], sucesss: sucesss, failure: failure);
    }
    class func reloadHomeData(options : GKLoadOptions){
        if GKHomeNetManager.manager.completion != nil {
            GKHomeNetManager.manager.completion(options);
        }
    }
    class func reloadHomeDataNeed(completion: @escaping ((_ options :GKLoadOptions) ->Void)){
        GKHomeNetManager.manager.completion = completion;
    }
}
